package fun.easycode.jointblock.service.executor;

import fun.easycode.jointblock.common.util.SecurityUtil;
import fun.easycode.jointblock.core.Executor;
import fun.easycode.jointblock.core.PageDto;
import fun.easycode.jointblock.manager.ArticleClientDto;
import fun.easycode.jointblock.manager.ArticleClientQuery;
import fun.easycode.jointblock.manager.ArticleServiceClient;
import fun.easycode.jointblock.service.dto.ArticlePagingQry;
import fun.easycode.jointblock.service.dto.data.ArticlePagingDto;
import fun.easycode.jointblock.service.dto.data.ArticlePagingDtoAssembler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xuzhe
 */
@Service
public class ArticlePagingQryExe implements Executor<ArticlePagingQry, PageDto<ArticlePagingDto>> {

    private final ArticleServiceClient articleServiceClient;

    @Autowired
    public ArticlePagingQryExe(ArticleServiceClient articleServiceClient){
        this.articleServiceClient = articleServiceClient;
    }

    @Override
    public PageDto<ArticlePagingDto> execute(ArticlePagingQry qry) {

        qry.validate();

        // 看自己还是看别人 OTHERS,代表着看别人的个人中心
        String status = !Objects.equals(qry.getUserId(), SecurityUtil.getCurrentUserId())
                ?"OTHERS":null;

        ResponseEntity<List<ArticleClientDto>> responseEntity = articleServiceClient.getRead(
                ArticleClientQuery.builder()
                        .userId(qry.getUserId())
                        .status(status)
                        .page(qry.getCurrent())
                        .size(qry.getSize())
                        .build());

        assert responseEntity.getBody() != null;


        return PageDto.toPageDto(qry, responseEntity, ArticlePagingDtoAssembler.INSTANCE::to);
    }

}
